package org.gongzhen.HomeService.impl;

import org.gongzhen.Category;
import org.gongzhen.HomeMapper.PostCollectionMapper;
import org.gongzhen.HomeMapper.PostLikeMapper;
import org.gongzhen.HomeMapper.PostMapper;
import org.gongzhen.HomeService.PostService;
import org.gongzhen.Like;
import org.gongzhen.Post;
import org.gongzhen.Save;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;
    @Autowired
    private PostLikeMapper postLikeMapper;
    @Autowired
    private PostCollectionMapper postCollectionMapper;


    /**
     * 收藏帖子
     */
    @Transactional(rollbackFor = Exception.class)
    public void collectPost(Integer postId) {
        // TODO 获取登录用户id
        Integer userId = 1;
        Post post = postMapper.getById(postId);
        // 一个用户只能收藏一次，再次点击是取消点赞
        Save pre = postCollectionMapper.getByUserIdAndPostId(postId, userId);
        if(pre != null){
            // 已收藏，取消收藏
            // 1.更新收藏数
            post.setCollectionCount(post.getCollectionCount()-1);
            // 2.删除记录
            postCollectionMapper.deleteById(pre.getId());
        }else{
            // 1.更新收藏数
            post.setCollectionCount(post.getCollectionCount()+1);
            // 2.绑定关系
            Save save = new Save();
            save.setCreateTime(LocalDateTime.now());
            save.setPostId(postId);
            save.setUserId(userId);
            postCollectionMapper.collection(save);
        }

        postMapper.update(post);
    }

    /**
     * 点赞帖子
     */
    @Transactional(rollbackFor = Exception.class)
    public void like(Integer postId) {
        // TODO 获取登录用户id
        Integer userId = 1;
        Post post = postMapper.getById(postId);
        // 一个用户只能点赞一次，再次点击是取消点赞
        Like pre = postLikeMapper.getByUserIdAndPostId(postId, userId);
        if(pre != null){
            // 已点赞，取消点赞
            // 1.更新点赞数
            post.setLikeCount(post.getLikeCount()-1);
            // 2.删除记录
            postLikeMapper.deleteById(pre.getId());
        }else{
            // 1.更新点赞数
            post.setLikeCount(post.getLikeCount()+1);
            // 2.绑定关系
            Like like = new Like();
            like.setCreateTime(LocalDateTime.now());
            like.setPostId(postId);
            like.setUserId(userId);
            postLikeMapper.like(like);
        }

        postMapper.update(post);
    }

    /**
     * 根据分类id获取所有帖子
     * @param categoryId
     * @return
     */
    public List<Post> getByCategoryId(Integer categoryId) {
        List<Post> categoryList = postMapper.getByCategoryId(categoryId);

        return categoryList;
    }

    /**
     * 修改帖子
     * 创作者可修改
     *
     * @param id
     * @param post
     */
    public void updateById(Integer id, Post post) {
        //TODO  1.获取帖子的currrntUserId,用baseContext
        Integer createrUserId  = post.getUserId();
        Integer currentUserId = 1;
        // 2.与当前登录用户id比较
        if(currentUserId != createrUserId){
            // 不相同
            throw new RuntimeException("不能修改他人的帖子");
        }else{
            // 相同
            // 1.补充基本字段
            post.setId(id);
            post.setUpdateTime(LocalDateTime.now());
            postMapper.update(post);
        }
    }

    /**
     * 根据id查询帖子
     * @param id
     */
    @GetMapping("/{id}")
    public Post getById(Integer id) {
        return postMapper.getById(id);
    }

    /**
     * 添加帖子
     * @param post
     */
    public void addPost(Post post) {
        // 1.补充基本属性
        post.setCreateTime(LocalDateTime.now());
        post.setUpdateTime(LocalDateTime.now());
        //TODO: 改为baseContext的id
        post.setUserId(1);
        // 2.调用mapper
        postMapper.insert(post);
    }

    /**
     * 根据id删除帖子
     * @param id
     */
    public void deleteById(Integer id) {

        postMapper.delete(id);
    }

    @Override
    public List<Post> getAllPost() {
        return postMapper.getAllPost();
    }

}
